package com.mobileClientKernel.android.serviceManager.network.requestexecutor;

import android.support.annotation.NonNull;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.mobileClientKernel.android.serviceManager.network.IRequest;
import com.mobileClientKernel.android.serviceManager.network.JJBaseRequest;
import com.mobileClientKernel.android.serviceManager.network.JJNetworkAgent;
import com.mobileClientKernel.android.commonLayer.JJLog;

import org.apache.http.Header;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

/**
 * Created by jj on 2018/5/10.
 */

public class VolleyRequest extends Request {

    private IRequest.ResponseListener mListener;
    private Map<String, String> mParams;
    private int responseCode;
    private Map<String, String> respHeader = new HashMap<>();
    private int index;

    private JJBaseRequest jjBaseRequest;

    public VolleyRequest(int index, int method, String url, Map<String, String> mParams, IRequest.ResponseListener listener, JJBaseRequest baseRequest) {
        super(method, url, null);
        this.index = index;
        this.mParams = mParams;
        this.mListener = listener;
        this.jjBaseRequest = baseRequest;
    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            responseCode = response.statusCode;
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            respHeader = response.headers;
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }

        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(Object response) {
        if (mListener != null) {
            Map<String, Object> responseMap = new HashMap();
            responseMap.put(JJNetworkAgent.RESPCODE, responseCode);
            responseMap.put(JJNetworkAgent.RESPCONTENT, response);
            responseMap.put(JJNetworkAgent.RESPHEADERS, respHeader);
            responseMap.put(JJNetworkAgent.REQINDEX, index);
            JJLog._d("VolleyRequest", "[deliverResponse]   " + response);
            mListener.onSuccess(responseMap);
        }
    }

    @Override
    public void deliverError(VolleyError error) {
        if (mListener != null) {
            Map<String, Object> responseMap = new HashMap();
            responseMap.put(JJNetworkAgent.RESPERROR, error.getMessage());
            if (error.networkResponse != null) {
                responseMap.put(JJNetworkAgent.RESPCODE, error.networkResponse.statusCode);
                responseMap.put(JJNetworkAgent.RESPHEADERS, error.networkResponse.headers);
            } else {
                responseMap.put(JJNetworkAgent.RESPCODE, -8888);
                Map<String, String> respHeader = new HashMap<>();
                responseMap.put(JJNetworkAgent.RESPHEADERS, respHeader);
            }

            responseMap.put(JJNetworkAgent.REQINDEX, index);
            mListener.onFailure(responseMap);
        }
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return this.mParams;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> header = this.getJjBaseRequest().getRequestHeader();
        if (header.size() > 0) {
            return header;
        } else {
            return super.getHeaders();
        }
    }

    @Override
    public int compareTo(@NonNull Object o) {
        return 0;
    }

    public long connectTimeoutMilliSecond() {
        return this.getJjBaseRequest().getConnectTimeoutMilliSecond();
    }

    public long readTimeoutMilliSecond() {
        return this.getJjBaseRequest().getReadTimeoutMilliSecond();
    }

    public long writeTimeoutMilliSecond() {
        return this.getJjBaseRequest().getWriteTimeoutMilliSecond();
    }

    public boolean isRetryOnConnectionFailure() {
        return this.getJjBaseRequest().isRetryOnConnectionFailure();
    }

    public HostnameVerifier getHostnameVerifier() {
        return this.getJjBaseRequest().getHostnameVerifier();
    }

    public JJBaseRequest getJjBaseRequest() {
        return jjBaseRequest;
    }

    public boolean isSslVerify() {
        return this.getJjBaseRequest().isSslVerify();
    }

    public SSLSocketFactory getSslSocketFactory() {
        return this.getJjBaseRequest().getSslSocketFactory();
    }

    public X509TrustManager getTrustManager() {
        return this.getJjBaseRequest().getTrustManager();
    }

    public void setJjBaseRequest(JJBaseRequest jjBaseRequest) {
        this.jjBaseRequest = jjBaseRequest;
    }
}
